package com.blog.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

//解释器
public class XmlParser {

	public static HashMap<String,String> parser(String xmlPath)
	{
		HashMap<String,String>hm=new HashMap<String,String>();
		String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取当前线程的路径
		xmlPath = basePath + xmlPath;
		try {
			//创建解释工厂；
			SAXParserFactory factory = SAXParserFactory.newInstance();
			//由工厂创建一个解释器；
			SAXParser saxParser;
			saxParser = factory.newSAXParser();
			//解释需要解释的文件； 
			File f=new File(xmlPath);
			//事件处理器；
			XmlHandler xh=new XmlHandler();
				saxParser.parse(f,xh);
				hm=xh.getHashMap();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

		return hm;
	}
	/*
	public static void main(String[] args) {
		String xmlPath="database.conf.xml";
		HashMap<String,String> hm = XmlParser.parser(xmlPath);
		//解释，测试
		System.out.println(hm.get("driver"));
		System.out.println(hm.get("url"));
		System.out.println(hm.get("user"));
		System.out.println(hm.get("password"));
	}
	*/
}






